<template>
  <div class="form-container">
    <el-form :model="form" label-width="120px" class="form">
      <!-- 选择所属集群 -->
      <el-form-item label="选择所属集群" required>
        <el-select v-model="form.cluster" placeholder="请选择集群" class="select">
          <el-option
            v-for="item in clusters"
            :key="item.clusterId"
            :label="item.clusterName"
          :value="item.clusterId"
          ></el-option>
        </el-select>
      </el-form-item>

      <!-- 下一步按钮 -->
      <el-form-item>
        <el-button type="primary" @click="handleNextStep" class="next-button">下一步</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { listClusters } from "@/api/nodes/clusters";

export default {
  data() {
    return {
      form: {
        cluster: '', // 绑定集群Id
      },
      clusters: [], // 存储集群列表
      loading: false, // 加载状态
    };
  },
  created() {
    this.getClusterList();
  },
  methods: {
    // 重置表单
    reset() {
      this.form.cluster = '';
    },

    // 获取集群列表
    getClusterList() {
      this.loading = true;
      listClusters().then(response => {
        if (response && response.rows && response.rows.length > 0) {
          this.clusters = response.rows;  // 正确赋值给clusters
        } else {
          this.clusters = [];  // 如果没有数据，清空集群列表
        }
        this.loading = false;
      }).catch(error => {
        this.loading = false;
        console.error("Error fetching clusters:", error);
      });
    },

    // 点击下一步
    handleNextStep() {
      if (this.form.cluster) {
        this.$router.push({
          name: 'createNodesItem',
          params: {clusterId: this.form.cluster}
        })
      } else {
        this.$message.error('请选择一个集群');
      }
    }
  },
};
</script>

<style scoped lang="scss">
.form-container {
  width: 400px;
  margin: 50px auto;
  background-color: #ffffff;
  border-radius: 10px;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
  padding: 30px;
}

.form {
  display: flex;
  flex-direction: column;
}

.el-form-item {
  margin-bottom: 20px;
}

.select {
  width: 100%;
}

.next-button {
  width: 100%;
  height: 40px;
  font-size: 16px;
  border-radius: 5px;
  transition: background-color 0.3s;
}

.next-button:hover {
  background-color: #409eff;
}

.el-select {
  border-radius: 5px;
  border: 1px solid #dcdfe6;
}

.el-select .el-input {
  border-radius: 5px;
}

.el-input__inner {
  font-size: 14px;
}

.el-form-item label {
  font-size: 14px;
  color: #333;
}
</style>
